package com.sqfw.project.wpReport.service.impl;

import com.sqfw.common.utils.StringUtils;
import com.sqfw.project.wp.domain.SysCode;
import com.sqfw.project.wp.domain.WpInfo;
import com.sqfw.project.wp.mapper.WpInfoMapper;
import com.sqfw.project.wp.service.ISysCodeService;
import com.sqfw.project.wpReport.domain.WpInfoBusiness;
import com.sqfw.project.wpReport.service.IWpInfoBusinessService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.*;
import java.util.function.Function;
import java.util.stream.Collectors;

@Slf4j
@Service
public class WpInfoBusinessServiceImpl implements IWpInfoBusinessService {

    @Autowired
    private ISysCodeService sysCodeService;

    @Autowired
    private WpInfoMapper wpInfoMapper;


    @Override
    public List<WpInfoBusiness> selectWpInfoBusinessList(WpInfo wpInfo) {

        List<SysCode> sysCodes = sysCodeService.selectSysCodeListToBusiness(new SysCode());
        //要返回的数据
        List<WpInfoBusiness> wpInfoBusinesses = new ArrayList<>();

        //内容分类查询条件
        List<List<String>> classAllFind = wpInfo.getClassAllFind();

        //内容分类查询
        if (StringUtils.isEmpty(classAllFind)) {
            wpInfo.setClassAll(null);
        } else {
            List<String> classAllCon = new ArrayList<>();
            for (List<String> strings : classAllFind) {
                classAllCon.add(strings.get(strings.size() - 1));
            }
            wpInfo.setClassAll(classAllCon);
        }

//        List<WpInfo> wpInfos = wpInfoMapper.selectWpInfoList(wpInfo);
        List<WpInfo> wpInfos = wpInfoMapper.selectWpInfoListPart(wpInfo);

        //class4
        Map<String, List<WpInfo>> class4Map = wpInfos
                .parallelStream()
                .filter(info -> StringUtils.isNotEmpty(info.getClass4()))
                .collect(Collectors.groupingBy(WpInfo::getClass4));
        if (class4Map.size() > 0) {

            //循环class4Map
            for (Map.Entry<String, List<WpInfo>> stringListEntry : class4Map.entrySet()) {
                List<WpInfo> value = stringListEntry.getValue();
//                wpInfos.removeAll(value);
                WpInfoBusiness wpInfoBusiness = this.getWpInfoBusiness(value, sysCodes);
                wpInfoBusinesses.add(wpInfoBusiness);

            }
        }
        //class3
        Map<String, List<WpInfo>> class3Map = wpInfos
                .parallelStream()
                .filter(info -> (StringUtils.isNotEmpty(info.getClass3()) && StringUtils.isEmpty(info.getClass4())))
                .collect(Collectors.groupingBy(WpInfo::getClass3));
        long c1 = System.currentTimeMillis();
        //循环class3Map
        for (Map.Entry<String, List<WpInfo>> stringListEntry : class3Map.entrySet()) {
            List<WpInfo> value = stringListEntry.getValue();
//            wpInfos.removeAll(value);
            WpInfoBusiness wpInfoBusiness = this.getWpInfoBusiness(value, sysCodes);
            wpInfoBusinesses.add(wpInfoBusiness);

        }
        //class2
        long d = System.currentTimeMillis();
        Map<String, List<WpInfo>> class2Map = wpInfos
                .parallelStream()
                .filter(info -> (StringUtils.isNotEmpty(info.getClass2()) &&
                        StringUtils.isEmpty(info.getClass3()) && StringUtils.isEmpty(info.getClass4())))
                .collect(Collectors.groupingBy(WpInfo::getClass2));

        //循环class2Map
        for (Map.Entry<String, List<WpInfo>> stringListEntry : class2Map.entrySet()) {
            List<WpInfo> value = stringListEntry.getValue();
//            wpInfos.removeAll(value);
            WpInfoBusiness wpInfoBusiness = this.getWpInfoBusiness(value, sysCodes);
            wpInfoBusinesses.add(wpInfoBusiness);

        }
        //class1
        Map<String, List<WpInfo>> class1Map = wpInfos
                .parallelStream()
                .filter(info -> (StringUtils.isNotEmpty(info.getClass1()) && StringUtils.isEmpty(info.getClass2()) &&
                        StringUtils.isEmpty(info.getClass3()) && StringUtils.isEmpty(info.getClass4())))
                .collect(Collectors.groupingBy(WpInfo::getClass1));

        //循环class1Map
        for (Map.Entry<String, List<WpInfo>> stringListEntry : class1Map.entrySet()) {
            List<WpInfo> value = stringListEntry.getValue();
//            wpInfos.removeAll(value);
            WpInfoBusiness wpInfoBusiness = this.getWpInfoBusiness(value, sysCodes);
            wpInfoBusinesses.add(wpInfoBusiness);

        }

        List<WpInfoBusiness> collect = wpInfoBusinesses.stream()
                .sorted(Comparator.comparing(checkNotNull(WpInfoBusiness::getClass1,"class1"))
                        .thenComparing(checkNotNull(WpInfoBusiness::getClass2,"class2"))
                        .thenComparing(checkNotNull(WpInfoBusiness::getClass3,"class3"))
                        .thenComparing(checkNotNull(WpInfoBusiness::getClass4,"class4")))
                .collect(Collectors.toList());





        if (StringUtils.isNotEmpty(collect)){
            WpInfoBusiness business = new WpInfoBusiness();

            business.setClass4name("合计");
            //咨询类
            int consultecount = wpInfoBusinesses.stream()
                    .map(WpInfoBusiness::getConsultecount)
                    .reduce(Integer::sum)
                    .orElse(0);
            business.setConsultecount(consultecount);

            //求助类
            int resortcount = wpInfoBusinesses.stream()
                    .map(WpInfoBusiness::getResortcount)
                    .reduce(Integer::sum)
                    .orElse(0);
            business.setResortcount(resortcount);
            //意见建议类
            int opinioncount = wpInfoBusinesses.stream()
                    .map(WpInfoBusiness::getOpinioncount)
                    .reduce(Integer::sum)
                    .orElse(0);
            business.setOpinioncount(opinioncount);

            //投诉举报类
            int complaintcount = wpInfoBusinesses.stream()
                    .map(WpInfoBusiness::getComplaintcount)
                    .reduce(Integer::sum)
                    .orElse(0);
            business.setComplaintcount(complaintcount);

            //表扬类
            int praisecount = wpInfoBusinesses.stream()
                    .map(WpInfoBusiness::getPraisecount)
                    .reduce(Integer::sum)
                    .orElse(0);
            business.setPraisecount(praisecount);

            //其他类
            int othercount = wpInfoBusinesses.stream()
                    .map(WpInfoBusiness::getOthercount)
                    .reduce(Integer::sum)
                    .orElse(0);
            business.setOthercount(othercount);

            //小计
            int totalcount = wpInfoBusinesses.stream()
                    .map(WpInfoBusiness::getTotalcount)
                    .reduce(Integer::sum)
                    .orElse(0);
            business.setTotalcount(totalcount);

            collect.add(business);

        }


        return collect;
    }


    private static <T, U> Function<T, U> checkNotNull(Function<T, U> function, String fieldName) {
        return t -> {
            U value = function.apply(t);
            if(value == null) {
                value = (U)"9999999";
            }

            return value;
        };
    }



    @Override
    public List<WpInfo> selectWpInfoByClassAndWpType(WpInfo wpInfo) {

        //内容分类查询条件
        List<List<String>> classAllFind = wpInfo.getClassAllFind();

        //内容分类查询
        if (StringUtils.isEmpty(classAllFind)) {
            wpInfo.setClassAll(null);
        } else {
            List<String> classAllCon = new ArrayList<>();
            for (List<String> strings : classAllFind) {
                classAllCon.add(strings.get(strings.size() - 1));
            }
            wpInfo.setClassAll(classAllCon);
        }

        if ("1".equals(wpInfo.getClassType())){
            //民政数据类型
            wpInfo.setClass1("13553");
        }
        if (StringUtils.isEmpty(wpInfo.getClass1())){
            wpInfo.setClass1(null);

        }
        if (StringUtils.isEmpty(wpInfo.getClass2())){

            return wpInfoMapper.selectCountWpInfoByWpType(wpInfo);

        }
        if (StringUtils.isEmpty(wpInfo.getClass3())){
            wpInfo.setClass3(null);
        }
        if (StringUtils.isEmpty(wpInfo.getClass4())){
            wpInfo.setClass4(null);
        }


        return wpInfoMapper.selectWpInfoByClassAndWpType(wpInfo);
    }

    public WpInfoBusiness getWpInfoBusiness(List<WpInfo> wpInfos, List<SysCode> sysCodes) {
        WpInfoBusiness wpInfoBusiness = new WpInfoBusiness();
        //小计
        wpInfoBusiness.setTotalcount(wpInfos.size());
        //咨询类
        Integer consultecount = 0;
        //求助类
        Integer resortcount = 0;
        //意见建议类
        Integer opinioncount = 0;
        //投诉举报类
        Integer complaintcount = 0;
        //表扬类
        Integer praisecount = 0;
        //其他类
        Integer othercount = 0;
        for (WpInfo info :  wpInfos) {
            if ("咨询类".equals(info.getWpType())) {
                consultecount++;
            }else if ("求助类".equals(info.getWpType())) {
                resortcount++;
            }else if ("意见建议类".equals(info.getWpType())) {
                opinioncount++;
            }else if ("投诉举报类".equals(info.getWpType())) {
                complaintcount++;
            }else if ("表扬类".equals(info.getWpType())) {
                praisecount++;
            }else if ("其他类".equals(info.getWpType())) {
                othercount++;
            }
        }
        wpInfoBusiness.setConsultecount(consultecount);
        wpInfoBusiness.setResortcount(resortcount);
        wpInfoBusiness.setOpinioncount(opinioncount);
        wpInfoBusiness.setComplaintcount(complaintcount);
        wpInfoBusiness.setPraisecount(praisecount);
        wpInfoBusiness.setOthercount(othercount);

//        //咨询类
//        List<WpInfo> consulteList = wpInfos.parallelStream()
//                .filter(info -> "咨询类".equals(info.getWpType()))
//                .collect(Collectors.toList());
//        wpInfoBusiness.setConsultecount(consulteList.size());
//
//        //求助类
//        List<WpInfo> resortList = wpInfos.parallelStream()
//                .filter(info -> "求助类".equals(info.getWpType()))
//                .collect(Collectors.toList());
//        wpInfoBusiness.setResortcount(resortList.size());
//        //意见建议类
//        List<WpInfo> opinionList = wpInfos.parallelStream()
//                .filter(info -> "意见建议类".equals(info.getWpType()))
//                .collect(Collectors.toList());
//        wpInfoBusiness.setOpinioncount(opinionList.size());
//
//        //投诉举报类
//        List<WpInfo> complaintList = wpInfos.parallelStream()
//                .filter(info -> "投诉举报类".equals(info.getWpType()))
//                .collect(Collectors.toList());
//        wpInfoBusiness.setComplaintcount(complaintList.size());
//
//        //表扬类
//        List<WpInfo> praiseList = wpInfos.parallelStream()
//                .filter(info -> "表扬类".equals(info.getWpType()))
//                .collect(Collectors.toList());
//        wpInfoBusiness.setPraisecount(praiseList.size());
//
//        //其他类
//        List<WpInfo> otherList = wpInfos.parallelStream()
//                .filter(info -> "其他类".equals(info.getWpType()))
//                .collect(Collectors.toList());
//        wpInfoBusiness.setOthercount(otherList.size());

        WpInfo wpInfo = wpInfos.stream().findAny().orElse(null);
        //TODO  确定返回的工单id，调试使用
        // wpInfoBusiness.setScids(wpInfos.stream().map(WpInfo::getScid).collect(Collectors.toList()));
        if (wpInfo != null) {
            String class1 = wpInfo.getClass1();
            String class2 = wpInfo.getClass2();
            String class3 = wpInfo.getClass3();
            String class4 = wpInfo.getClass4();
            wpInfoBusiness.setClass1(class1);
            wpInfoBusiness.setClass2(class2);
            wpInfoBusiness.setClass3(class3);
            wpInfoBusiness.setClass4(class4);
            for (SysCode code : sysCodes) {
                if (StringUtils.isNotEmpty(class1) && code.getAutoId().toString().equals(class1)) {
                    wpInfoBusiness.setClass1name(code.getNodename());
                }
                if (StringUtils.isNotEmpty(class2) && code.getAutoId().toString().equals(class2)) {
                    wpInfoBusiness.setClass2name(code.getNodename());
                    if (StringUtils.isNotEmpty(code.getFiveMajorModules())){
                        wpInfoBusiness.setFiveMajorModules(code.getFiveMajorModules());
                    }
                }
                if (StringUtils.isNotEmpty(class3) && code.getAutoId().toString().equals(class3)) {
                    wpInfoBusiness.setClass3name(code.getNodename());
                    if (StringUtils.isNotEmpty(code.getFiveMajorModules())){
                        wpInfoBusiness.setFiveMajorModules(code.getFiveMajorModules());
                    }
                }
                if (StringUtils.isNotEmpty(class4) && code.getAutoId().toString().equals(class4)) {
                    wpInfoBusiness.setClass4name(code.getNodename());
                }
            }

//            if (StringUtils.isNotEmpty(class1)){
//                Long class1L = Long.valueOf(class1);
//                sysCodes.parallelStream()
//                        .filter(code-> class1L.equals(code.getAutoId()))
//                        .findFirst()
//                        .ifPresent(syscode->wpInfoBusiness.setClass1name(syscode.getNodename()));
//            }
//            if (StringUtils.isNotEmpty(class2)){
//                Long class2L = Long.valueOf(class2);
//                sysCodes.parallelStream()
//                        .filter(code-> class2L.equals(code.getAutoId()))
//                        .findFirst()
//                        .ifPresent(syscode->{
//                            wpInfoBusiness.setClass2name(syscode.getNodename());
//                            if (StringUtils.isNotEmpty(syscode.getFiveMajorModules())){
//                                wpInfoBusiness.setFiveMajorModules(syscode.getFiveMajorModules());
//                            }
//                        });
//
//            }
//            if (StringUtils.isNotEmpty(class3)){
//                Long class3L = Long.valueOf(class3);
//                sysCodes.parallelStream()
//                        .filter(code-> class3L.equals(code.getAutoId()))
//                        .findFirst()
//                        .ifPresent(syscode->{
//                            wpInfoBusiness.setClass3name(syscode.getNodename());
//                            if (StringUtils.isNotEmpty(syscode.getFiveMajorModules())){
//                                wpInfoBusiness.setFiveMajorModules(syscode.getFiveMajorModules());
//                            }
//                        });
//            }
//            if (StringUtils.isNotEmpty(class4)){
//                Long class4L = Long.valueOf(class4);
//                sysCodes.parallelStream()
//                        .filter(code-> class4L.equals(code.getAutoId()))
//                        .findFirst()
//                        .ifPresent(syscode->wpInfoBusiness.setClass4name(syscode.getNodename()));
//            }
        }

        return wpInfoBusiness;
    }
}
